package com.birdy.dto;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author birdy
 */
public class Response {
    public static final int SUCCESS = 200;
    public static final int FAILED = -201;
    public static final int SQL_EXCEPTION = -801;

    private int res;
    private String msg;
    private Map<String, Object> data;


    public Response() {
    }

    public Response(int res, String msg, Map<String, Object> data) {
        this.res = res;
        this.msg = msg;
        this.data = data;
    }

    public static Response commonResponseByRes(int res, String failMsg) {
        if (res == 0) {
            return Response.builder()
                    .res(Response.SUCCESS)
                    .msg("success")
                    .build();
        }
        else {
            return Response.builder()
                    .res(Response.FAILED)
                    .msg(failMsg)
                    .build();
        }
    }
    public static Response commonResponseByRes(boolean success, String failMsg) {
        if (success) {
            return Response.builder()
                    .res(Response.SUCCESS)
                    .msg("success")
                    .build();
        }
        else {
            return Response.builder()
                    .res(Response.FAILED)
                    .msg(failMsg)
                    .build();
        }
    }
    public static Response success(String msg) {
        return Response.builder().res(Response.SUCCESS).msg(msg).build();
    }
    public static Response success() {
        return Response.builder().res(Response.SUCCESS).msg("success").build();
    }
    public static Response fail(String msg) {
        return Response.builder().res(Response.FAILED).msg(msg).build();
    }
    public static Response responseWithMessage(boolean isSuccess, String msg) {
        if (isSuccess) {
            return Response.builder()
                    .res(Response.SUCCESS)
                    .msg(msg)
                    .build();
        }
        else {
            return Response.builder()
                    .res(Response.FAILED)
                    .msg(msg)
                    .build();
        }
    }
    public int getRes() {
        return res;
    }

    public void setRes(int res) {
        this.res = res;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()){
            return false;
        }
        Response response = (Response) o;
        return res == response.res && Objects.equals(msg, response.msg) && Objects.equals(data, response.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(res, msg, data);
    }

    @Override
    public String toString() {
        return "Response{" +
                "res=" + res +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public static ResponseBuilder builder() {
        return new ResponseBuilder();
    }
    public static class ResponseBuilder {
        private int res;
        private String msg;
        private final Map<String, Object> data;

        ResponseBuilder() {
            this.data = new HashMap<>();
        }

        public ResponseBuilder res(final int res) {
            this.res = res;
            return this;
        }

        public ResponseBuilder msg(final String msg) {
            this.msg = msg;
            return this;
        }

        public ResponseBuilder data(String key, Object val) {
            this.data.put(key, val);
            return this;
        }

        public Response build() {
            return new Response(this.res, this.msg, this.data);
        }

        @Override
        public String toString() {
            return "Response.ResponseBuilder(res=" + this.res + ", msg=" + this.msg + ", data=" + this.data + ")";
        }
    }
}
